//
//  HeaderView.m
//  QQ好友列表
//
//  Created by Jorn.Wu on 15/5/25.
//  Copyright (c) 2015年 Jorn.Wu. All rights reserved.
//

#import "HeaderView.h"

@interface HeaderView ()
    
@property (nonatomic, weak) UIButton *nameBtn;

@property (nonatomic, weak) UILabel *numberOfonline;


@end

@implementation HeaderView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
+ (instancetype)headerViewWith:(UITableView *)tableView{
    NSString *ID = @"headerVeiw";//类方法一般不用static
    HeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:ID];
    //这里应该注意，header的frame还要设置，否则下面添加东西到它上面，没法正常显示
    
    if (headerView == nil) {
        headerView = [[HeaderView alloc]initWithReuseIdentifier:ID];
    }
    return headerView;
    
}
- (instancetype)initWithReuseIdentifier:(NSString *)reuseIdentifier{
    
    if (self = [super initWithReuseIdentifier:reuseIdentifier]) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        [btn setImage:[UIImage imageNamed:@"buddy_header_arrow"] forState:UIControlStateNormal];//小箭头
        btn.imageView.contentMode = UIViewContentModeCenter;//图片居中（这是指里面的内容）
        btn.imageView.clipsToBounds = NO;//默认是剪掉超出部分，在这不要让他剪,所有view都有这个属性，在这注意是设置imageView，不要然图片被剪切
        
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
        btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//设置居左(是指垂直水平方向)
        
        btn.contentEdgeInsets          = UIEdgeInsetsMake(0, 10, 0, 0);//按钮的内边距
        
        btn.titleEdgeInsets            = UIEdgeInsetsMake(0, 10, 0, 0);//按钮上文字的内边距
        
        [self addSubview:btn];
        self.nameBtn                   = btn;
        
        
        [btn addTarget:self action:@selector(headerBtnAtion) forControlEvents:UIControlEventTouchUpInside];//添加点击事件
        
////
        
        UILabel *label        = [[UILabel alloc]init];
        
//        label.backgroundColor = [UIColor grayColor];
        
        label.textAlignment   = NSTextAlignmentCenter;//局中
        
        [self addSubview:label];
        
        self.numberOfonline   = label;
        
    }
    return self;
}

- (void)headerBtnAtion{
    self.fgModel.open = !self.fgModel.open;
    
    if (self.block) {//是否存在
        self.block(self);//是把改变后的self传过去，然后再控制器里刷新列表
    }
    
}

- (void)didMoveToSuperview{//当  当前的View  加载到父控件的时候调用
    if (self.fgModel.open) {//如果是展开的时候才朝下
        self.nameBtn.imageView.transform = CGAffineTransformMakeRotation(M_PI_2);
    }
}


- (void)setFgModel:(FriendsGroupModel *)fgModel{//注意在这fgModel有值，而self.model没值，因为重写了set方法
    _fgModel = fgModel;//把值放到属性中，在其他处才能调用
    [self.nameBtn setTitle:self.fgModel.name forState:UIControlStateNormal];//组名
    self.numberOfonline.text = [NSString stringWithFormat:@"%ld/%ld",fgModel.online,fgModel.friends.count];//在线人数设置
}


- (void)layoutSubviews{//当前的View的frame发生一些改变时，调用此方法来改变其子控件或子视图做相应的改变
    
    CGFloat labelY = 0;
    CGFloat labelW = 50;
    CGFloat labelH = self.frame.size.height;
    CGFloat labelX = self.frame.size.width - labelW - 5;

    self.numberOfonline.frame = CGRectMake(labelX, labelY, labelW, labelH);
    self.nameBtn.frame        = self.bounds;

}


@end









